home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 1.iso / toolbox / src / tutorials / custEducation / opengl1 / examples / lighting / lightIntensity.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-11-11  |  8.8 KB  |  353 lines

  1. /* Copyright 1996 Silicon Graphics, Inc.
  2.  * All Rights Reserved.
  3.  *
  4.  * This is UNPUBLISHED PROPRIETARY SOURCE CODE of Silicon Graphics, Inc.;
  5.  * the contents of this file may not be disclosed to third parties, copied or
  6.  * duplicated in any form, in whole or in part, without the prior written
  7.  * permission of Silicon Graphics, Inc.
  8.  *
  9.  * RESTRICTED RIGHTS LEGEND:
  10.  * Use, duplication or disclosure by the Government is subject to restrictions
  11.  * as set forth in subdivision (c)(1)(ii) of the Rights in Technical Data
  12.  * and Computer Software clause at DFARS 252.227-7013, and/or in similar or
  13.  * successor clauses in the FAR, DOD or NASA FAR Supplement. Unpublished -
  14.  * rights reserved under the Copyright Laws of the United States.
  15.  */
  16.  
  17. /* lightIntensity.c - Use light properties to change the intensity
  18.  *      of the light in our scene.
  19.  *
  20.  *  Left Mouse Button        - change incidence and azimuth angles
  21.  *  Middle Mousebutton        - change the twist angle based on
  22.  *                  horizontal mouse movement
  23.  *  Right Mousebutton        - zoom in and out based on vertical
  24.  *                  mouse movement
  25.  *  <a> Key            - toggle ambient light intensity on/off
  26.  *  <d> Key            - toggle diffuse light intensity on/off
  27.  *  <s> Key            - toggle specular light intensity on/off
  28.  *  <R> Key            - reset viewpoint
  29.  *  Escape key            - exit the program
  30.  */
  31.  
  32. #include <GL/gl.h>
  33. #include <GL/glu.h>
  34. #include <GL/glut.h>
  35.  
  36. #include <math.h>
  37. #include <stdio.h>
  38.  
  39. /*  Function Prototypes  */
  40.  
  41. GLvoid  initgfx( GLvoid );
  42. GLvoid  drawScene( GLvoid );
  43. GLvoid  reshape( GLsizei, GLsizei );
  44. GLvoid  keyboard( GLubyte, GLint, GLint );
  45. GLvoid  mouse( GLint, GLint, GLint, GLint );
  46. GLvoid  motion( GLint, GLint );
  47.  
  48. GLvoid toggleAmbient( GLvoid );
  49. GLvoid toggleDiffuse( GLvoid );
  50. GLvoid toggleSpecular( GLvoid );
  51.  
  52. void resetView( GLvoid );
  53. void polarView( GLfloat, GLfloat, GLfloat, GLfloat );
  54. void printHelp( char * );
  55.  
  56. /* Global Definitions */
  57.  
  58. #define KEY_ESC    27    /* ascii value for the escape key */
  59.  
  60. /* Global Variables */
  61.  
  62. static enum        actions { MOVE_EYE, TWIST_EYE, ZOOM, MOVE_NONE };
  63. static GLint        action;
  64.  
  65. static GLdouble        xStart = 0.0, yStart = 0.0;
  66.  
  67. static GLfloat         fovy, near, far; 
  68. static GLfloat        distance, twistAngle, incAngle, azimAngle;
  69.  
  70. static GLfloat         angle = 0.0;
  71.  
  72. void
  73. main( int argc, char *argv[] )
  74. {
  75.     GLsizei width, height;
  76.  
  77.     glutInit( &argc, argv );
  78.  
  79.     width = glutGet( GLUT_SCREEN_WIDTH ); 
  80.     height = glutGet( GLUT_SCREEN_HEIGHT );
  81.     glutInitWindowPosition( width / 4, height / 4 );
  82.     glutInitWindowSize( (width / 2) - 4, height / 2 );
  83.     glutInitDisplayMode( GLUT_RGBA | GLUT_DEPTH | GLUT_DOUBLE );
  84.     glutCreateWindow( argv[0] );
  85.  
  86.     initgfx();
  87.  
  88.     glutMouseFunc( mouse );
  89.     glutMotionFunc( motion );
  90.     glutKeyboardFunc( keyboard );
  91.     glutReshapeFunc( reshape );
  92.     glutDisplayFunc( drawScene ); 
  93.  
  94.     printHelp( argv[0] );
  95.  
  96.     glutMainLoop();
  97. }
  98.  
  99. void
  100. printHelp( char *progname )
  101. {
  102.     fprintf(stdout, "\n%s - demonstrate light intensity properties\n\n" 
  103.         "Left Mousebutton    - move eye position\n"
  104.         "Middle Mousebutton    - change twist angle\n"
  105.         "Right Mousebutton    - move up / down to zoom in / out\n"
  106.         "<a> Key    - toggle ambient light intensity on/off\n"
  107.         "<d> Key    - toggle diffuse light intensity on/off\n"
  108.         "<s> Key    - toggle specular light intensity on/off\n"
  109.         "<R> Key        - reset viewpoint\n"
  110.         "Escape Key        - exit the program\n\n",
  111.         progname);
  112. }
  113.  
  114. GLvoid
  115. initgfx( GLvoid )
  116. {
  117.     glClearColor( 0.0, 0.0, 0.0, 1.0 );
  118.     glEnable( GL_DEPTH_TEST );
  119.  
  120.     fovy = 60.0;    /* field of view in Y */
  121.     near = 3.0;    /* Near clipping plane location */
  122.     far  = 12.0;    /* Far clipping plane location */
  123.     resetView();
  124.  
  125.     /* Turn on a default light */
  126.     glEnable( GL_LIGHT0 );
  127. }
  128.  
  129. GLvoid
  130. toggleAmbient( GLvoid )
  131. {
  132.     /* Set up intensities for the light */
  133.     static GLfloat    ambientIntensity[] = { 0.2, 0.0, 0.2, 1.0 };
  134.     static GLfloat    defaultAmbient[] = { 0.0, 0.0, 0.0, 1.0 };
  135.  
  136.     static GLboolean useDefault = GL_TRUE;
  137.  
  138.     useDefault = !useDefault;
  139.  
  140.     if (useDefault)
  141.         glLightfv( GL_LIGHT0, GL_AMBIENT, defaultAmbient );
  142.     else 
  143.         glLightfv( GL_LIGHT0, GL_AMBIENT, ambientIntensity );
  144. }
  145.  
  146. GLvoid
  147. toggleDiffuse( GLvoid )
  148. {
  149.     static GLfloat    diffuseIntensity[] = { 0.0, 0.1, 1.0, 1.0 };
  150.     static GLfloat    defaultDiffuse[] = { 1.0, 1.0, 1.0, 1.0 };
  151.  
  152.     static GLboolean useDefault = GL_TRUE;
  153.  
  154.     useDefault = !useDefault;
  155.     if (useDefault)
  156.         glLightfv( GL_LIGHT0, GL_DIFFUSE, defaultDiffuse );
  157.     else 
  158.         glLightfv( GL_LIGHT0, GL_DIFFUSE, diffuseIntensity );
  159. }
  160.  
  161. GLvoid
  162. toggleSpecular( GLvoid )
  163. {
  164.     static GLfloat    specularIntensity[] = { 1.0, 0.0, 1.0, 1.0 };
  165.     static GLfloat    defaultSpecular[] = { 1.0, 1.0, 1.0, 1.0 };
  166.  
  167.     static GLboolean useDefault = GL_TRUE;
  168.  
  169.     useDefault = !useDefault;
  170.     if (useDefault)
  171.         glLightfv( GL_LIGHT0, GL_SPECULAR, defaultSpecular );
  172.     else 
  173.         glLightfv( GL_LIGHT0, GL_SPECULAR, specularIntensity );
  174. }
  175.  
  176. GLvoid 
  177. keyboard( GLubyte key, GLint x, GLint y )
  178. {
  179.     switch (key) {
  180.     case 'a':    /* toggle ambient intensity */
  181.         toggleAmbient();
  182.         break;
  183.     case 'd':    /* toggle Diffuse intensity */
  184.         toggleDiffuse();
  185.         break;
  186.     case 's':    /* toggle Specular intensity */
  187.         toggleSpecular();
  188.         break;
  189.     case 'R':
  190.         resetView();
  191.         glutPostRedisplay();
  192.         break;
  193.     case KEY_ESC:    /* Exit whenever the Escape key is pressed */
  194.         exit(0);
  195.     }
  196.     glutPostRedisplay();
  197. }
  198.  
  199. GLvoid 
  200. mouse( GLint button, GLint state, GLint x, GLint y )
  201. {
  202.     static GLint buttons_down = 0;
  203.  
  204.     if (state == GLUT_DOWN) {
  205.         switch (button) {
  206.         case GLUT_LEFT_BUTTON:
  207.             action = MOVE_EYE;
  208.             break;
  209.         case GLUT_MIDDLE_BUTTON:
  210.             action = TWIST_EYE;
  211.             break;
  212.         case GLUT_RIGHT_BUTTON:
  213.             action = ZOOM;
  214.             break;
  215.         }
  216.  
  217.         /* Update the saved mouse position */
  218.         xStart = x;
  219.         yStart = y;
  220.     } else {
  221.         if (--buttons_down == 0) 
  222.             action = MOVE_NONE;
  223.     }
  224. }
  225.  
  226. GLvoid
  227. motion( GLint x, GLint y )
  228. {
  229.     switch (action) {
  230.     case MOVE_EYE:
  231.         /* Adjust the eye position based on the mouse position */
  232.         azimAngle += (GLdouble) (x - xStart);
  233.         incAngle -= (GLdouble) (y - yStart);
  234.         break;
  235.     case TWIST_EYE:
  236.         /* Adjust the eye twist based on the mouse position */
  237.         twistAngle = fmodf(twistAngle+(x - xStart), 360.0);
  238.         break;
  239.     case ZOOM:
  240.         /* Adjust the eye distance based on the mouse position */
  241.         distance -= (GLdouble) (y - yStart)/10.0;
  242.         break;
  243.     default:
  244.         printf("unknown action %d\n", action);
  245.     }
  246.     
  247.     /* Update the stored mouse position for later use */
  248.     xStart = x;
  249.     yStart = y;
  250.  
  251.     glutPostRedisplay();
  252.  
  253. }
  254.  
  255. void
  256. resetView( GLvoid )
  257. {
  258.     distance = near + (far - near) / 2.0;
  259.     twistAngle = 0.0;    /* rotation of viewing volume (camera) */
  260.     incAngle = 60.0;
  261.     azimAngle = 0.0;
  262.     fovy = 60.0;    /* Field of view in Y angle */
  263. }
  264.  
  265. GLvoid
  266. reshape( GLsizei width, GLsizei height )
  267. {
  268.     GLdouble    aspect;
  269.  
  270.     glViewport( 0, 0, width, height );
  271.  
  272.     aspect = (GLdouble) width / (GLdouble) height;
  273.  
  274.     glMatrixMode( GL_PROJECTION );
  275.     glLoadIdentity();
  276.     gluPerspective( fovy, aspect, near, far );
  277.     glMatrixMode( GL_MODELVIEW );
  278. }
  279.  
  280. void
  281. polarView( GLfloat distance, GLfloat azimuth, GLfloat incidence,
  282.             GLfloat twist)
  283. {
  284.     glTranslatef( 0.0, 0.0, -distance);
  285.     glRotatef( -twist, 0.0, 0.0, 1.0);
  286.     glRotatef( -incidence, 1.0, 0.0, 0.0);
  287.     glRotatef( -azimuth, 0.0, 0.0, 1.0);
  288. }
  289.  
  290. GLvoid
  291. drawScene( GLvoid )
  292. {
  293.     /* Define a few materials properties */
  294.     GLfloat   redAmbient[] = { 0.3, 0.1, 0.1, 1.0 };
  295.     GLfloat   redDiffuse[] = { 1.0, 0.0, 0.0, 1.0 };
  296.     GLfloat   blueAmbient[] = { 0.1, 0.1, 0.3, 1.0 };
  297.     GLfloat   blueDiffuse[] = { 0.0, 0.0, 1.0, 1.0 };
  298.     GLfloat   yellowDiffuse[] = { 1.0, 1.0, 0.0, 1.0 };
  299.     GLfloat   yellowEmission[] = { 0.6, 0.6, 0.0, 1.0 };
  300.     GLfloat   defaultEmission[] = { 0.0, 0.0, 0.0, 1.0 };
  301.     GLfloat   whiteSpecular[] = { 1.0, 1.0, 1.0, 1.0 };
  302.     GLfloat   greenSpecular[] = { 0.0, 1.0, 0.0, 1.0 };
  303.     GLfloat   defaultSpecular[] = { 0.0, 0.0, 0.0, 1.0 };
  304.  
  305.     glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT );
  306.  
  307.     glPushMatrix();
  308.         polarView( distance, azimAngle, incAngle, twistAngle );
  309.         XYZaxes();
  310.  
  311.         glEnable( GL_LIGHTING );
  312.  
  313.         glMaterialfv( GL_FRONT, GL_EMISSION, defaultEmission );
  314.  
  315.         /* Set properties for a shiny red material,
  316.          * with a green highlight */
  317.         glMaterialfv( GL_FRONT, GL_AMBIENT, redAmbient );
  318.         glMaterialfv( GL_FRONT, GL_DIFFUSE, redDiffuse );
  319.         glMaterialfv( GL_FRONT, GL_SPECULAR, greenSpecular );
  320.         glMaterialf( GL_FRONT, GL_SHININESS, 128.0 );
  321.         glPushMatrix();
  322.             glTranslatef( -2.0, 1.5, 0.0 );
  323.             glutSolidSphere( 0.7, 31, 31 );
  324.         glPopMatrix();
  325.  
  326.         /* Set properties for a dull blue material with
  327.          *   a small white highlight */
  328.         glMaterialfv( GL_FRONT, GL_AMBIENT, blueAmbient );
  329.         glMaterialfv( GL_FRONT, GL_DIFFUSE, blueDiffuse );
  330.         glMaterialfv( GL_FRONT, GL_SPECULAR, whiteSpecular );
  331.         glMaterialf( GL_FRONT, GL_SHININESS, 20.0 );
  332.         glPushMatrix();
  333.             glTranslatef( 2.5, 0.0, 0.0 );
  334.             glutSolidTorus( 0.25, 0.75, 16, 31 );
  335.         glPopMatrix();
  336.  
  337.         /* Set properties for a yellow glowing material */
  338.         glMaterialfv( GL_FRONT, GL_AMBIENT_AND_DIFFUSE, yellowDiffuse);
  339.         glMaterialfv( GL_FRONT, GL_EMISSION, yellowEmission );
  340.         glMaterialfv( GL_FRONT, GL_SPECULAR, defaultSpecular );
  341.  
  342.         glPushMatrix();
  343.             glTranslatef( 0.0, 2.0, 2.0 );
  344.             glutSolidCube( 0.5 );
  345.         glPopMatrix();
  346.  
  347.         glDisable( GL_LIGHTING );
  348.  
  349.     glPopMatrix();
  350.  
  351.     glutSwapBuffers();
  352. }
  353.